<?php

declare(strict_types=1);

use think\migration\Migrator;

class CreateRegionTables extends Migrator
{
    public function change(): void
    {
        $this->createRegionTable();
        $this->createRegionUserTable();
    }

    private function createRegionTable(): void
    {
        $this->table('region', [
            'comment' => '区域定义表',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('name', 'string', [
                'limit' => 80,
                'comment' => '区域名称',
            ])
            ->addColumn('code', 'string', [
                'limit' => 60,
                'comment' => '区域编码',
            ])
            ->addColumn('parent_id', 'integer', [
                'default' => 0,
                'comment' => '父区域ID',
            ])
            ->addColumn('level', 'tinyinteger', [
                'default' => 1,
                'comment' => '层级 1-大区 2-省份',
            ])
            ->addColumn('path', 'string', [
                'limit' => 255,
                'default' => '',
                'comment' => '路径 /1/3/',
            ])
            ->addColumn('sort', 'integer', [
                'default' => 0,
                'comment' => '排序',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-停用 1-启用',
            ])
            ->addColumn('seller_customer_count', 'integer', [
                'default' => 0,
                'comment' => '关联卖家数量',
            ])
            ->addColumn('buyer_customer_count', 'integer', [
                'default' => 0,
                'comment' => '关联买家数量',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['code'], ['unique' => true])
            ->addIndex(['parent_id'])
            ->addIndex(['level'])
            ->addIndex(['status'])
            ->create();
    }

    private function createRegionUserTable(): void
    {
        $this->table('region_user', [
            'comment' => '区域与用户关联（备用）',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
        ])
            ->addColumn('region_id', 'integer', [
                'signed' => false,
                'comment' => '区域ID',
            ])
            ->addColumn('user_id', 'integer', [
                'signed' => false,
                'comment' => '用户ID',
            ])
            ->addColumn('role_type', 'string', [
                'limit' => 20,
                'default' => 'operator',
                'comment' => '角色类型，预留',
            ])
            ->addColumn('is_primary', 'tinyinteger', [
                'signed' => false,
                'default' => 0,
                'comment' => '是否主区域',
            ])
            ->addColumn('status', 'tinyinteger', [
                'signed' => false,
                'default' => 1,
                'comment' => '状态 0-禁用 1-启用',
            ])
            ->addColumn('config', 'json', [
                'null' => true,
                'comment' => '扩展配置',
            ])
            ->addTimestamps()
            ->addColumn('delete_time', 'timestamp', [
                'null' => true,
                'comment' => '删除时间',
            ])
            ->addIndex(['region_id'])
            ->addIndex(['user_id'])
            ->addIndex(['region_id', 'user_id'], ['unique' => true])
            ->addIndex(['role_type'])
            ->create();
    }
}
